//全选伪数组
var checkAllEles = document.querySelectorAll(".check-all");
//单选伪数组
var checkOneEles = document.querySelectorAll(".check-one");
//单价伪数组
var priceEles = document.querySelectorAll(".price");
//总价伪数组
var subEles = document.querySelectorAll(".subtotal");
//添加按钮伪数组
var addEles = document.querySelectorAll(".add");
//减少伪数组
var reduceEles = document.querySelectorAll(".reduce");
//数量
var countEles = document.querySelectorAll(".count-input");
//总价
var priceTotal = document.getElementById("priceTotal");
//选择的总量
var selectedTotal = document.getElementById("selectedTotal");
//全删
var deleteAll = document.getElementById("deleteAll");
//单删伪数组
var delBtns = document.querySelectorAll(".delete");
//所有tr标签伪数组
var trEles = document.querySelectorAll("tbody tr");

countEles.forEach(function( countEle , key ){
    // console.log(priceEles[key].innerHTML);
    addEles[key].onclick = function (){
        countEle.value = parseInt( countEle.value ) + 1;
        if ( countEle.value > 1 ){
            reduceEles[key].innerHTML = "-";
        }
        var price = priceEles[key].innerHTML ; 
        var num = countEle.value ; 
        subEles[key].innerHTML = `${price*100*num/100}`
        totalCount();
    }
    reduceEles[key].onclick = function (){
        if ( parseInt(countEle.value) > 1 ){
            countEle.value = parseInt( countEle.value ) - 1;
        }
        if ( parseInt(countEle.value) == 1 ){
            reduceEles[key].innerHTML = "";
        }
        var price = priceEles[key].innerHTML ; 
        var num = countEle.value ; 
        subEles[key].innerHTML = `${price*100*num/100}`
        totalCount();
    }
});

checkAllEles.forEach(function(checkAllEle){
    checkAllEle.onclick = function () {
        var flag = checkAllEle.checked ;
        checkOneEles.forEach(function(checkOneEle){
            checkOneEle.checked = flag ;
        });
        checkAllEles.forEach(function(checkAllEle){
            checkAllEle.checked = flag ;
        });
        totalCount();
    }
});

checkOneEles.forEach(function(checkOneEle){
    checkOneEle.onclick = function () {
        var res = [...checkOneEles].every(function(item){
            return item.checked ;
        });
        checkAllEles.forEach(function(checkAllEle){
            checkAllEle.checked = res ;
        });
        totalCount();
    }
});

delBtns.forEach(function(delBtn,key){
    delBtn.onclick = function () {
        trEles[key].remove();
        totalCount();
    }
});

deleteAll.onclick = function () {
    checkOneEles.forEach(function(checkOneEle,key){
        if(checkOneEle.checked){
            trEles[key].remove();
        }
        var checkOneEles = document.querySelectorAll(".check-one");
        // console.log(checkOneEles.length);
        if( checkOneEles.length == 0 ){
            checkAllEles.forEach(function(checkAllEle){
                checkAllEle.checked = false ; 
            });
        }
    });
    totalCount();
}


function totalCount(){
    var checkOneEles = document.querySelectorAll(".check-one");
    var priceEles = document.querySelectorAll(".price");
    var countEles = document.querySelectorAll(".count-input");
    var priceTotal = document.getElementById("priceTotal");
    var selectedTotal = document.getElementById("selectedTotal");
    var subEles = document.querySelectorAll(".subtotal");
    var totalCount = 0 ;
    var totalPrice = 0 ;
    checkOneEles.forEach(function(checkOneEle,key){
        if (checkOneEle.checked){
            totalCount = totalCount + parseInt( countEles[key].value ) ;  
            totalPrice = totalPrice  + parseInt( subEles[key].innerHTML ) ;               
        }
    });
    selectedTotal.innerHTML = totalCount ; 
    priceTotal.innerHTML = totalPrice ; 
}
